<?php

use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Form;
use Dcat\Admin\Grid\Filter;
use Dcat\Admin\Layout\Navbar;

/**
 * Dcat-admin - admin builder based on Laravel.
 * @author jqh <https://github.com/jqhph>
 *
 * Bootstraper for Admin.
 *
 * Here you can remove builtin form field:
 *
 * extend custom field:
 * Dcat\Admin\Form::extend('php', PHPEditor::class);
 * Dcat\Admin\Grid\Column::extend('php', PHPEditor::class);
 * Dcat\Admin\Grid\Filter::extend('php', PHPEditor::class);
 *
 * Or require js and css assets:
 * Admin::css('/packages/prettydocs/css/styles.css');
 * Admin::js('/packages/prettydocs/js/main.js');
 *
 */
Admin::css('/vendor/mango/main.css');

Grid::resolving(function (Grid $grid) {
    if (env('IS_DEMO') == true) {
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->disableEditButton();
    }
    $grid->setActionClass(\Dcat\Admin\Grid\Displayers\Actions::class); // 列中操作直接显示 不要默认的三个点
    $grid->showColumnSelector(); // 开启右边的字段显示选择
    // $grid->model()->orderBy("id", "desc");
    $grid->disableViewButton(); // 禁用详情按钮
    $grid->showQuickEditButton();  // 开启快速编辑 
    $grid->enableDialogCreate(); // 启用弹窗创建

    $grid->actions(function (\Dcat\Admin\Grid\Displayers\Actions $actions) {
        $actions->disableView();
        $actions->disableEdit();
    });
    $grid->option("dialog_form_area", ["75%", "100%"]);
});

Form::resolving(function (Form $form) {
    if (env('IS_DEMO') == true) {
        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();
        $form->disableSubmitButton();
    }
});

Filter::resolving(function (Filter $filter) {
    $filter->panel(); // 不以默认的侧边显示搜索   直接当前页显示
    // $filter->expand();// 是否显示搜索条件 默认不显示 点击筛选后才出现 设置后就直接显示
});


Admin::navbar(function (Navbar $navbar) {
      // 全屏按钮
      $navbar->right(
        <<<HTML
            <ul class="nav navbar-nav">
                <li class="nav-item">
                    &nbsp;
                    <a style="cursor: pointer" onClick="
                        let full = document.fullscreenElement;
                        //切换为全屏模式
                        if (!full) {
                            //文档根节点的方法requestFullscreen,实现全屏模式
                            document.documentElement.requestFullscreen();
                        } else {
                            //变为不是全屏模式->退出全屏模式
                            document.exitFullscreen();
                        }
                    ">
                        <i class="feather icon-maximize" style="font-size: 1.2rem"></i>
                    </a> 
                    &nbsp; &nbsp; 
                </li>
            </ul> 
HTML
    );
    // 搜索框
    $navbar->right(
        <<<HTML
        <div class="input-group input-group-sm" style="width: 160px">
            <input type="text" name="q" class="form-control" placeholder="搜索">
            <span class="input-group-append">
                <button type="submit" class="btn btn-primary" style="height:30px"
                onClick="
                    // 遍历所有菜单项
                    $(document).ready(function() {
                        // 查询菜单并跳转
                        let search = $('input[name=q]').val().trim();
                        if (search == '') {
                            return false;
                        }
                        var searchResult = false;
                        $('ul.nav li.nav-item a').parents('li.nav-item').removeClass('menu-open');
                        $('ul.nav li.nav-item a').removeClass('active');
                        $('ul.nav li.nav-item a').each(function() {
                            var linkText = $(this).find('p').text().trim(); // 获取链接中的文本
                            if (linkText.toLowerCase().includes(search)) {
                                $(this).parents('li.nav-item').find('ul.nav-treeview').slideDown('slow');
                                $(this).parents('li.nav-item').addClass('menu-open');
                                $(this).addClass('active');
                                searchResult = true;
                            }
                        });
                        if (!searchResult) {
                            $('input[name=q]').val('')
                            // 搜索结果不存在
                            layer.msg('未找到相关菜单');
                        }
                    });

                "><i class="fa fa-search"></i></button>
            </span>
        </div>
HTML
    );


});